package com.greenantmall.activity;

import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.adapter.MyMsgListAdapter;
import com.greenantmall.bean.MyMsg;
import com.greenantmall.utils.SQLiteHelper;
import com.greenantmall.utils.iLog;
import com.greenantmall.view.CommonDialog;
import com.lsarah.utils.SystemBarTintManager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class MyMsgActivity extends BaseActivity
{

    private Context context;
    private TextView noMsgHint;
    private ListView favlist;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        context = this;
        
        SystemBarTintManager.setContentView(this, R.layout.mymsg, getResources().getColor(R.color.green));
        favlist = (ListView) findViewById(R.id.favlist);// 得到ListView对象的引用
        noMsgHint = (TextView) findViewById(R.id.noMsgHint);
        String userId = "";
        AVUser user = AVUser.getCurrentUser();
        if (user != null)
        {
            userId = user.getObjectId();
        }
        final List<MyMsg> strs = SQLiteHelper.getInstance(this).getMsgs(userId);

        iLog.d("", "SQLiteHelper:" + strs.size());
        if(strs.size()==0)
        {
            noMsgHint.setVisibility(View.VISIBLE);
            favlist.setVisibility(View.INVISIBLE);
        }
        else{
            noMsgHint.setVisibility(View.INVISIBLE);
            favlist.setVisibility(View.VISIBLE);  
        }
        
        final MyMsgListAdapter myMsgListAdapter = new MyMsgListAdapter(strs, context);

        favlist.setAdapter(myMsgListAdapter);

        favlist.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
                long arg3)
            {
                // 点击后在标题上显示点击了第几行 setTitle("你点击了第"+arg2+"行");
                CommonDialog errDialog = new CommonDialog(
                    context,
                    R.style.ExitDialogStyle,
                    context.getString(R.string.common_title),
                    context.getString(R.string.delete_hint));
                errDialog
                    .setSureListener(new CommonDialog.OnSureClickListener()
                {

                    @Override
                    public void sureClicked()
                    {
                        SQLiteHelper.getInstance(context).deleteMsg(strs.get(arg2).getMsg());
                        strs.remove(arg2);
                        myMsgListAdapter.notifyDataSetChanged();
                        if(strs.size()==0)
                        {
                            refreshView();
                        }
                    }
                });
                errDialog.show();
            }
        });

        this.findViewById(R.id.add).setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                CommonDialog errDialog = new CommonDialog(
                    context,
                    R.style.ExitDialogStyle,
                    context.getString(R.string.common_title),
                    context.getString(R.string.delete_hint_1));
                errDialog
                    .setSureListener(new CommonDialog.OnSureClickListener()
                {

                    @Override
                    public void sureClicked()
                    {
                    	String userId = "";
                        AVUser user = AVUser.getCurrentUser();
                        if (user != null)
                        {
                            userId = user.getObjectId();
                        }
                        SQLiteHelper.getInstance(context).deleteAllMsg(userId);
                        strs.clear();
                        myMsgListAdapter.notifyDataSetChanged();
                        refreshView();
                    }
                });
                errDialog.show();

            }
        });

    }
    public void refreshView(){
        noMsgHint.setVisibility(View.VISIBLE);
        favlist.setVisibility(View.INVISIBLE);
    }
    public void goBack(View view)
    {
        finish();
    }
}
